<?php

namespace app\merchants\controller;

use app\BaseController;
use app\merchants\loader\Services;
use think\Exception;
use yiqiniu\extend\library\GlobalParam;

class ApiBase extends BaseController
{
    /**
     * @var mixed
     */
    protected $adminUser = [
        'admin_id' => 0,
        'sub_admin_ids' => [],
    ];

    /**
     * 商户ID
     * @var mixed
     */
    protected $merId = '';
    protected $request_app = 9;

    /**
     * 初始化
     */
    protected function initialize(): void
    {
        try {
            $this->request_app = $this->request->request_app ?? 9;
            if (!empty($this->request->adminUser)) {
                $this->adminUser = $this->request->adminUser;
                $this->merId = $this->adminUser['mer_id'];
                $groupInfo = Services::MerchantRole()->groupInfoSimple($this->adminUser["group_id"]);
                $this->adminUser["data_permission"] = $groupInfo["data_permission"];
                $this->adminUser["is_sys"] = $groupInfo["is_sys"];
                $this->adminUser["house_permission"] = !empty($groupInfo["house_permission"]) ? explode(",", $groupInfo["house_permission"]) : [];
                switch ($groupInfo["data_permission"]) {
                    case 2:
                        $this->adminUser["sub_admin_ids"][] = $this->adminUser["admin_id"];
                        break;
                    case 1:
                        $this->adminUser["sub_admin_ids"] = Services::MerchantDepartment()->merchantDeptSubIds($this->adminUser["mer_id"], $this->adminUser["department_id"]);
                        break;
                    default:
                        $this->adminUser["sub_admin_ids"] = [];
                }
                $obj = GlobalParam::getInstance();
                $obj->put('mer_id', $this->adminUser["mer_id"]);
                $obj->put('op_user', $this->adminUser["admin_id"]);
                $obj->put('op_name', ($this->adminUser['admin_name'] ?? ''));
            }
        } catch (Exception $e) {
            api_result($e);
        }
    }
}
